﻿using System.Collections.Generic;
using System.Linq;

namespace Eksponent.RazorRocks
{
    public class TypeInfo
    {
        /// <summary>
        /// Gets or sets the name of the type.
        /// </summary>
        /// <value>
        /// The name.
        /// </value>
        public string Name { get; set; }

        /// <summary>
        /// Gets or sets the base types, interfaces and what not for the type in the order they should be evaluated when finding templates.
        /// </summary>
        /// <value>
        /// The base names.
        /// </value>
        public string[] BaseNames { get; set; }

        public TypeInfo(string name, IEnumerable<string> baseNames = null)
        {
            Name = name;
            if (baseNames != null)
            {
                BaseNames = baseNames.ToArray();
            }
        }

        internal IEnumerable<string> AllNames
        {
            get
            {
                var list = new List<string>();
                if( !string.IsNullOrEmpty(Name) )
                {
                    list.Add(Name);
                }                
                if (BaseNames != null)
                {
                    list.AddRange(BaseNames.Where(x=>!string.IsNullOrEmpty(x)));
                }
                return list;
            }
        }

        public override bool Equals(object obj)
        {
            var other = obj as TypeInfo;
            if( other != null )
            {
                return other.Name.Equals(Name);
            }
            return base.Equals(obj);
        }

        public override int GetHashCode()
        {
            return Name.GetHashCode();           
        }

        public static implicit operator TypeInfo(string name)
        {
            return new TypeInfo(name);
        }
    }
}